package com.rabbitmq.workfair;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.entity.User;
import com.rabbitmq.util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Send {
    private static String queue_name = "simple_name";
    public static void main(String[] args) throws IOException, TimeoutException {
        //1、获得连接
        Connection connection = ConnectionUtil.getConnection();
        //2、创建通道
        Channel channel = connection.createChannel();
        //3、申明一个队列
        channel.queueDeclare(queue_name,false,false,false,null);

        //4、准备待发送的数据
        String message = "欢迎学习消息队列RabbitMQ!";

        //5、发送消息
        for (Long i = 0L; i < 30; i++) {
            User user = new User();
            user.setId(i);
            user.setName("张三");
            message = JSON.toJSONString(user);
            channel.basicPublish("",queue_name,null,message.getBytes());
        }

        //6、发送完数据关闭通道、连接
        channel.close();
        connection.close();

    }
}
